<?php

namespace App\Http\Controllers\Common;

use App\Package\Code\Code;
use Illuminate\Http\Request;
use App\Package\Message\Message;
use App\Http\Controllers\Controller;

/**
 * 通知短息逻辑控制器
 *
 * Date: 10/04/2018
 * @author George
 * @package App\Http\Controllers\Common
 */
class MessageController extends Controller
{
	/**
	 * 发送验证码
	 *
	 * Date: 10/04/2018
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Psr\SimpleCache\InvalidArgumentException
	 */
	public function sendCode(Request $request)
	{
		$this->validate($request, [
			'mobile' => ['required', 'regex:/^(1[3|4|5|6|7|8|9])\d{9}$/']
		], [
			'mobile.required' => '请填写您的手机号码',
			'mobile.regex' => '手机号码格式错误'
		]);

		$mobile = $request->get('mobile');
		$message = new Message();
		$code = Code::generator();
		$response = $message->code($mobile, $code);

		if ($response->code == 2) {
			return message('验证码已发送');
		}
		return failed((string) $response->msg);
	}
}
